#include "CVSView.h"
#include "CGame.h"
#include "Macros.h"
namespace Framework
{
	CVSView::CVSView(HINSTANCE _hInstance,int width,int height,bool isFullScreen,char* name):m_iWidth(width),m_iHeight(height),m_isFullScreen(isFullScreen),m_hWnd(0),m_hWndDC(0)
	{
		SetInstance(this);
		hInstance=_hInstance;
		if (name)
		{
			m_strTitle=new char[strlen(name)];
			strcpy(m_strTitle,name);
		} 
		else
		{
			char* tmp="Untitle";
			m_strTitle=new char[strlen(tmp)];
			strcpy(m_strTitle,tmp);
		}
		InitWindow(); 
		CDXManager::GetInstance()->InitDirectX(m_hWnd);
		CDXManager::GetInstance()->InitKeyboardDevice(hInstance);
	}

	CVSView::~CVSView(void)
	{
		Destroy();
		SAFE_DEL(m_strTitle);		
	}

	void CVSView::InitWindow()
	{
		WNDCLASSEX wcex;
		wcex.cbClsExtra=0;
		wcex.cbSize=sizeof(WNDCLASSEX);
		wcex.cbWndExtra=0;
		wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
		wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
		wcex.hIcon=NULL;
		wcex.hIconSm=NULL;
		wcex.hInstance=hInstance;
		wcex.lpfnWndProc=WndProc;
		wcex.lpszClassName="CVSView";
		wcex.lpszMenuName=NULL;
		wcex.style=CS_HREDRAW|CS_VREDRAW;
		if (!RegisterClassEx(&wcex))
		{
			return;
		}

		DWORD style=WS_POPUP;
		if (!m_isFullScreen)
		{
			style=WS_SYSMENU|WS_BORDER|WS_CAPTION|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
		}
		RECT clientSize;
		clientSize.top=0;
		clientSize.left=0;
		clientSize.right=m_iWidth;
		clientSize.bottom=m_iHeight;
		AdjustWindowRect(&clientSize,style,false);

		const int realWidth=clientSize.right-clientSize.left;
		const int realHeigh=clientSize.bottom-clientSize.top;

		int wndLeft=(GetSystemMetrics(SM_CXSCREEN)-realWidth)/2;
		int wndRight=(GetSystemMetrics(SM_CYSCREEN)-realHeigh)/2;
		if (!m_isFullScreen)
		{
			m_hWnd= CreateWindow("CVSView",m_strTitle,style,wndLeft,wndRight,realWidth,realHeigh,NULL,NULL,hInstance,NULL);
		} 
		else
		{
			m_hWnd=CreateWindow("CVSView",m_strTitle,style,0,0,m_iWidth,m_iHeight,NULL,NULL,hInstance,NULL);
		}
		m_hWndDC=GetDC(m_hWnd);
		ShowWindow(m_hWnd,SW_SHOW);
		UpdateWindow(m_hWnd);
		if (!m_isFullScreen)
		{
			MoveWindow(m_hWnd,wndLeft,wndRight,realWidth,realHeigh,true);
		}
		SetActiveWindow(m_hWnd);
		SetForegroundWindow(m_hWnd);
		if (m_isFullScreen)
		{
			DEVMODE dm;
			memset(&dm,0,sizeof(dm));
			dm.dmSize=sizeof(DEVMODE);
			dm.dmFields=DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL;
			dm.dmPelsWidth=m_iWidth;
			dm.dmPelsHeight=m_iHeight;
			dm.dmBitsPerPel=32;
			ChangeDisplaySettings(&dm,CDS_FULLSCREEN);
		}
	}
	LRESULT CALLBACK CVSView::WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
	{
		switch(message)
		{
		case WM_CREATE:
			return 0;
			break;
		case WM_PAINT:
			PAINTSTRUCT ps;
			BeginPaint(hWnd,&ps);
			EndPaint(hWnd,&ps);
			return 0;
			break;
		case WM_DESTROY:
			CGame::GetInstance()->Exit();
			PostQuitMessage(0);
			break;
		}
		return DefWindowProc(hWnd,message,wParam,lParam);
	}
	void CVSView::Destroy()
	{
		if (m_isFullScreen)
		{
			ChangeDisplaySettings(NULL,0);
		}
	}
	void CVSView::Update()
	{
		while(true)
		{
			MSG msg;
			ZeroMemory(&msg,sizeof(msg));
			while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
			{
				TranslateMessage(&msg);
				if (msg.hwnd==m_hWnd)
				{
					WndProc(m_hWnd,msg.message,msg.wParam,msg.lParam);
				}
			}
			CGame::GetInstance()->Update();
			if (!CGame::GetInstance()->IsAlive())
			{
				return;
			}
			DispatchMessage(&msg);
		}
	}

}